package com.eshop.service.impl.mongodb;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import sun.util.logging.resources.logging;

import com.eshop.common.constant.CoreConstant;
import com.eshop.common.util.date.DateUtils;
import com.eshop.dao.mongodb.EOrderDao;
import com.eshop.frameworks.core.dao.DAO;
import com.eshop.frameworks.core.entity.PageEntity;
import com.eshop.frameworks.core.service.impl.AbstractService;
import com.eshop.model.mongodb.EOrder;
import com.eshop.service.mongodb.EOrderService;

@Service("eorderService")
public class EOrderServiceImpl extends AbstractService<EOrder, String>
		implements EOrderService {

	@Autowired
	private EOrderDao eorderDao;

	@Override
	public DAO<EOrder, String> getDao() {
		return eorderDao;
	}

	@Override
	public void changeOrderStauts(String id, int stauts) {
		Query query = new Query();
		query.addCriteria(Criteria.where("id").is(id));
		Update update = Update.update("status", stauts).set("modifyTime",
				new Date());
		eorderDao.update(query, update, EOrder.class);
	}

	@Override
	public List<EOrder> getMyBills(String shopperId, int status) {
		Query query = new Query(Criteria.where("userId").is(shopperId));
		if (status != CoreConstant.EORDER_STATUS_All) {
			query.addCriteria(Criteria.where("status").is(status));
		}
		List<Order> orders = new ArrayList<Order>();
		orders.add(new Order(Direction.DESC, "createTime"));
		query.with(new Sort(orders));
		List<EOrder> eorders = eorderDao.findList(query, EOrder.class);
		return eorders;
	}

	@Override
	public List<EOrder> getMyBillsByPage(EOrder eorder, PageEntity page,
			String searchTime) {
		Query query = new Query();
		if (eorder.getUserId() != null && !"".equals(eorder.getUserId())) {
			query.addCriteria(Criteria.where("userId").is(eorder.getUserId()));
		} else if (eorder.getShopperId() != null
				&& !"".equals(eorder.getShopperId())) {
			query.addCriteria(Criteria.where("shopperId").is(
					eorder.getShopperId()));
		} else {
			// TODO 用户为空，错误，或返回所有订单
		}
		if (eorder.getStatus() != null
				&& eorder.getStatus() != CoreConstant.EORDER_STATUS_All) {
			query.addCriteria(Criteria.where("status").is(eorder.getStatus()));
		}
		if (!"".equals(searchTime) && searchTime != null) {
			String pattern = "yyyy-MM-dd HH:mm:ss";
			String firseDay = searchTime + "-01 00:00:00";
			Date firseDate = DateUtils.toDate(firseDay, pattern);
			Date lastDate = DateUtils.toDate(
					DateUtils.getEndDateOfMonth(firseDay) + " 23:59:59",
					pattern);
			query.addCriteria(Criteria.where("orderTime").gte(firseDate).lte(lastDate));
		}
		int count = (int) this.getMyBillsCount(query);
		page.setTotalResultSize(count);
		List<Order> orders = new ArrayList<Order>();
		orders.add(new Order(Direction.DESC, "orderTime"));
		query.with(new Sort(orders));
		query.skip(page.getStartRow()).limit(page.getPageSize());
		List<EOrder> eorders = eorderDao.findList(query, EOrder.class);
		return eorders;
	}

	@Override
	public long getMyBillsCount(Query query) {
		return eorderDao.size(query, EOrder.class);
	}
}
